import base64
import io
from PIL import Image

# 图片转换成base64
def image_to_base64(path):
    with open(path, 'rb') as img:
        # 使用base64进行编码
        b64encode = base64.b64encode(img.read())
        s = b64encode.decode()
        b64_encode = 'data:image/jpeg;base64,%s' % s
        # 返回base64编码字符串
        return b64_encode


#打开图片
def open_image(img_b64decode):
    image = io.BytesIO(img_b64decode)
    print(image)
    img = Image.open(image)
    img.show()


# base64转换成图片
def base64_to_image(base64_encod_str):
    res=base64_encod_str.split(',')[1]
    img_b64decode = base64.b64decode(res)
    open_image(img_b64decode) #打开图片验证下
    #保存图片
    with open("1.jpg", 'wb') as png:
        png.write(img_b64decode)
        
b64 = ""

base64_to_image(b64)